# coding:utf-8
import pandas as pd
import numpy as np
import datetime
import sys
import time


LabelDay = datetime.datetime(2014,12,18,0,0,0)
dir_drop1112 = "/home/rjs/文档/Datasets/tianchi/"
Dataset_drop1112 = dir_drop1112 + "drop1112_sub_item.csv"
Data = pd.read_csv(Dataset_drop1112)
Data['daystime'] = Data['days'].map(lambda x: time.strptime(x, "%Y-%m-%d")).map(lambda x: datetime.datetime(*x[:6]))


result_dir = "../result/"
predicted = pd.read_csv(result_dir + 'result.csv')

reference = Data[Data['daystime'] == (LabelDay + datetime.timedelta(days=1))]
reference = reference[reference['behavior_type'] == 4]  # 购买的记录
reference = reference[['user_id', 'item_id']]  # 获取ui对
reference = reference.drop_duplicates(['user_id', 'item_id'])  # 去重
ui = predicted['user_id'] / predicted['item_id']

predicted = predicted[ui.duplicated() == False]

predicted_ui = predicted['user_id'] / predicted['item_id']
reference_ui = reference['user_id'] / reference['item_id']

is_in = predicted_ui.isin(reference_ui)
true_positive = predicted[is_in]

tp = len(true_positive)
predictedSetCount = len(predicted)
referenceSetCount = len(reference)

precision = tp / predictedSetCount
recall = tp / referenceSetCount

f_score = 2 * precision * recall / (precision + recall)

tp = recall * referenceSetCount
predictedSetCount = tp / precision

print('%.8f%% %.8f %.8f %.0f %.0f' %
      (f_score * 100, precision, recall, tp, predictedSetCount))